<?php

require_once WACT_ROOT . 'controller/controller.inc.php';
require_once APP_ROOT . "view/common/layout.formview.php";
require_once APP_ROOT . "view/common/layout.view.php";
require_once WACT_ROOT . 'controller/form.inc.php';
require_once WACT_ROOT . 'validation/rules/match.inc.php';
require_once WACT_ROOT . 'validation/rules/member.inc.php';
require_once WACT_ROOT . 'validation/rules/pattern.inc.php';
require_once APP_ROOT .'model/common/user.php';
require_once APP_ROOT .'model/common/state.php';
require_once APP_ROOT .'model/common/sportmail.php';
require_once APP_ROOT .'model/common/session.php';
require_once APP_ROOT . "model/common/parameters.php";
require_once APP_ROOT . "model/common/price.php";
require_once APP_ROOT . "model/common/site.php";


class RegisterView extends LayoutFormView{

    function RegisterView($TemplateFile) {
        parent :: LayoutFormView($TemplateFile);

    }

    function prepare(&$controller, &$request, &$responseModel) {
    	parent::prepare();

    	if(APP_NAME!=""){
			$this->Template->set('app_name',APP_NAME);
		} else $this->Template->set('app_name','.');

        $this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - Register');      

      
     	$states =& $this->Template->getChild('state');
     	$states_list =& State::getStates();
        $states->setChoices($states_list);
  		$state = $responseModel->get('state');
        $states->setSelection($state);

        $price1 = number_format(Price::getPrice(1),2);
        $price2 = number_format(Price::getPrice(2),2);
        $price3 = number_format(Price::getPrice(3),2);
        $price4 = number_format(Price::getPrice(4),2);

        $responseModel->set('price1',$price1);
        $responseModel->set('price2',$price2);
        $responseModel->set('price3',$price3);
        $responseModel->set('price4',$price4);
    }
}

class RegisterViewError extends RegisterView{

    function RegisterViewError($TemplateFile) {
        parent :: RegisterView($TemplateFile);
    }

    function prepare(&$controller, &$request, &$responseModel) {
    	parent::prepare(&$controller, &$request, &$responseModel);
		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
    
     	$this->Template->set('logerror',"User with such username already registered.");
    }
}

class RegisterPage extends PageController {
    var $usernameerr=0;
    function RegisterPage() {
        parent::PageController();

        $Form =& new FormController();
 		$Form->addChild('register', new ButtonController(new Delegate($this, 'register')));
 		$Form->addChild('subscribe', new ButtonController(new Delegate($this, 'subscribe')));
		$Form->setDefaultChild('register');

		$Form->addRule(new RequiredRule('firstname'));
		$Form->addRule(new RequiredRule('lastname'));

		$Form->addRule(new RequiredRule('email'));
		$Form->addRule(new PatternRule('email','/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i'));

		$Form->addRule(new Handle('RequiredRule', array('login')));
		$Form->addRule(new PatternRule('login','/^[a-z]{1}[a-z0-9_]+$/'));

		$Form->addRule(new RequiredRule('password'));
		$Form->addRule(new RequiredRule('confpswd'));
		//rul for zip
		$zipRule=& new PatternRule('zip','/^[0-9]{4,5}$/');
		$zipRule->setGroup('ziprule');
		$Form->addRule($zipRule);

		$Form->addRule(new PatternRule('home_phone','/^[0-9- +]{11,}$/'));
		$Form->addRule(new PatternRule('work_phone','/^[0-9- +]{11,}$/'));
		$Form->addRule(new MatchRule('password', 'confpswd'));
		$rule =& new RequiredRule('agree');
		$rule->setGroup('inputexample');
		$Form->addRule($rule);


        $this->addChild("register_form",$Form);
		$this->setDefaultChild('register_form');


        $site_id = Site::getTemplateSiteID();
        $mytemplate = 'pub/register.html';
 

        $reg_view =& new RegisterView ($mytemplate);
		$this->addView('view',$reg_view);
		$this->addView('viewer',new RegisterViewError ($mytemplate));
        $this->addView('success', WACT_DEFAULT_VIEW);
        $this->addView('subscribe', new Handle('RedirectView', array('/subscribe')));
        $this->addView('thanks', new Handle('RedirectView', array('/thankjoining')));
		//$this->setDefaultView(new RegisterView ('/pub/register.html'));
        $this->setDefaultView($reg_view);


    }

    function register(&$source, &$request, &$responseModel){
    	require_once APP_ROOT .'model/common/user.php';
    	$login = $request->getPostProperty('login');

    	$user=&User::getRecordbyLogin($login);

    	if($user!=null) {
    		return 'viewer';
    	}
    	else if($responseModel->isValid()){

    		$id=User::insertRegisteredUser($responseModel);
    		$user=&User::getRecord($id);
    		Session::setUser($user);

    		//send mail:
    		Sportmail::sendRegisterMail($id);

    		$dosubscribe = $request->getPostProperty('dosubscribe');
            if ( $dosubscribe ) return 'subscribe';
            else {
                return 'thanks';
    			//$redirect_url='http://'.$_SERVER['HTTP_HOST'].'/thankjoining.htm';
    	 		//if(defined('SID')&&(!isset($_COOKIE[session_name()])))
    	 		//$redirect_url.='?'.SID;
    	 		//header('Location: '.$redirect_url);
    	 	}

    	}
    }

    function subscribe(&$source, &$request, &$responseModel){
    	require_once APP_ROOT .'model/common/user.php';
    	$login=$request->getPostProperty('login');
    	$user=&User::getRecordbyLogin($login);

    	if($user!=null) {
    		return 'viewer';
    	}
    	else if($responseModel->isValid()){

    		$id=User::insertRegisteredUser($responseModel);
    		$user=&User::getRecord($id);
    		Session::setUser($user);

    		//send mail:
    		Sportmail::sendRegisterMail($id);
    		return 'subscribe';
    		//$redirect_url='http://'.$_SERVER['HTTP_HOST'].'/subscribe.htm';
    	 	//if(defined('SID')&&(!isset($_COOKIE[session_name()])))
    	 	//$redirect_url.='?'.SID;
    	 	//header('Location: '.$redirect_url);

    	}
    }

}


class ThankView extends LayoutView {

    function ThankView($TemplateFile) {
        parent :: View($TemplateFile);
    }

    function prepare() {
    	parent::prepare();
        $this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - Thank you for joining!');      
 
     	$user_id=Session::getUserId();
     	$user=User::getRecord($user_id);
     	$login=$user->get('login');
     	$password=$user->get('password');
     	//$this->Template->set('user_id',$user_id);
     	$this->Template->set('login',$login);
     	$this->Template->set('password',$password);
    }
}



class ThankjoiningPage extends PageController {

    function ThankjoiningPage() {
        parent::PageController();

        $registerpage="http://".$_SERVER['HTTP_HOST'].'/register.htm';
        $registerpage2="http://".$_SERVER['HTTP_HOST'].'/'.APP_NAME.'/register.htm';

        $from=getenv("HTTP_REFERER");
        $isok = false;
        //print $from;
        if (!is_null($from)) {
        	$arrayfrom=explode('?',$from);
        	$from = $arrayfrom[0];
        	if($from==$registerpage || $from==$registerpage2)  $isok = true;
        }

        $is_login = Session::isLogin();
        if (!$is_login) $isok = false;


        $mytemplatethanks = 'pub/thankjoining.html';
        $mytemplatenotfound = 'pub/notfound.html';

        if ( $isok ) $this->setDefaultView(new ThankView($mytemplatethanks));
        else $this->setDefaultView(new NotFoundView($mytemplatenotfound));



    }


}

?>
